﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections;

namespace HelloXNA2.Component.MyCustom.Mouse
{
    public class drawLineHelper
    {
        GraphicsDevice graphicsDevice;
        Texture2D pixel;

        public Color Colour;

        public Vector2 Position;

        public float Depth;

        ArrayList vectors;

        public drawLineHelper(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;

            pixel = new Texture2D(graphicsDevice, 1, 1, false, SurfaceFormat.Color);
            Color[] pixels = new Color[1];
            pixels[0] = Color.White;
            pixel.SetData<Color>(pixels);

            Colour = Color.White;
            Position = Vector2.Zero;
            Depth = 0;

            vectors = new ArrayList();
        }

        public void CreateLine(Vector2 from, Vector2 to, int sides)
        {
            vectors.Clear();
            var vect = from - to;
            var count = Vector2.Distance(from, to);

            vect.Normalize();

            for (int i = 0; i < (int)count; i++)
            {
                vectors.Add(new Vector2(from.X - vect.X * i, from.Y - vect.Y * i));
            }

            var dWith = vect / sides;
        }

        public void Render(SpriteBatch spriteBatch)
        {
            if (vectors.Count < 2)
                return;
            for (int i = 1; i < vectors.Count; i++)
            {
                Vector2 vector1 = (Vector2)vectors[i - 1];
                Vector2 vector2 = (Vector2)vectors[i];
                // calculate the distance between the two vectors
                float distance = Vector2.Distance(vector1, vector2);
                // calculate the angle between the two vectors
                float angle = (float)Math.Atan2((double)(vector2.Y - vector1.Y),
                    (double)(vector2.X - vector1.X));
                //stretch the pixel between the two vectors
                spriteBatch.Draw(pixel,
                    Position + vector1,
                    null,
                    Colour,
                    angle,
                    Vector2.Zero,
                    new Vector2(distance, 1),
                    SpriteEffects.None,
                    Depth);

                //spriteBatch.Draw(pixel, new Rectangle(100, 100, 100, 100), Color.White);
            }
        }
    }
}
